﻿namespace BEGDC.CC.Web.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Common;
	using BEGDC.Utility.Castle;

	/// <summary>
	///		Summary description for Export.
	/// </summary>
	public class Export : System.Web.UI.UserControl
	{
		private int id;
		private string type;
		protected System.Web.UI.HtmlControls.HtmlTable tb_Export;
		protected System.Web.UI.HtmlControls.HtmlAnchor btn_Export;
		private string transition;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				if (Request.QueryString["AppID"]!=null && Request.QueryString["AppID"] != "")
					this.id=Convert.ToInt32(Request.QueryString["AppID"]);
				else
					this.id=0;
			
				if (this.id<=0)
				{
					BEGDC.CC.Web.UserControls.IPageAccess page = (UserControls.IPageAccess)this.Page;
					this.id=page.Access.AppId;
				}
				this.type =Request.QueryString["PDType"];
			
				if (this.id <= 0)
				{
					this.Visible=false;
					return;
				}	//this.btnExport.Visible=false;
				
				this.Visible=true;;
				//this.btnExport.Visible=true;

				string url = "../Workflow/ExcelExport.aspx";
				if (id > 0)
					url += "?AppID="+this.id.ToString();

				if (this.type != null)
					url += "&PDType="+this.type;
				
				if (this.type.ToUpper()=="CR")
				{
					this.transition=Request.QueryString["Transition"];
					if (this.transition != null)
					{
						url += "&Transition="+this.transition;
					}
				}
                
				btn_Export.HRef = url;
			}
			catch
			{
				this.tb_Export.Visible=false;
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnExport_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if (this.id<=0)
			{
				Page.RegisterClientScriptBlock("","<script>alert('Please save the application form first.\\nIf you already saved, please contact the system admin.');</script>");						
				return;
			}
			ExcelTool.ICCExcelExport excelExport=null;
			BEGDC.CC.BL.Common.Query.IQuery queryService =  AppContainer.Instance[ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;


			try
			{
				switch (type)
				{
					case "EV":
						excelExport=new ExcelTool.ExportEVForm();
						break;
					case "CV":
						excelExport=new ExcelTool.ExportCVForm();
						break;
					case "CO":
						excelExport=new ExcelTool.ExportCOForm();
						break;
					case "CR":
						Application app=queryService.GetApplication(this.id);
						if (app.IsTransition)
						{	
							//CR-A
							excelExport=new ExcelTool.ExportCRFormA();
						}
						else
						{
							//CR-B
							excelExport=new ExcelTool.ExportCRFormB();
						}						
						break;
					default:
						break;
			
				}
				excelExport.Export(this.Page,this.id);
			}
			catch
			{
                throw;
                //Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}
		}
	}
}
